package business.service.user.impl;

import java.io.IOException;

import business.entity.User;
import business.service.user.IUserLoginService;

import app.MyApplication;

import ui.model.user.UserLoginViewModel;
import util.web.WebServiceHelper;

public class UserLoginService implements IUserLoginService{
	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public UserLoginService(UserLoginViewModel model) {
		this.model = model;
		url = MyApplication.url + "/login";
	}


	public UserLoginViewModel getModel() {
		return model;
	}


	public void setModel(UserLoginViewModel model) {
		this.model = model;
	}


	private String url = null;
	private UserLoginViewModel model = null;
	
	
	@Override
	public boolean login(User user) {
		model.setUser(user);
		model.clearError();
		if(model.verifyModel() == null){
			//TODO 这里下次迭代再改成动态的
			String msg = "username=" + user.getName() +
					     "&password=" + user.getPasswd();
			try {
				String result = WebServiceHelper.post(url, msg);
				if(result.equals("true")){
					return true;
				}else{
					model.addError("name", "用户或者密码错误");
				}
			} catch (IOException e) {
				model.addError("network", "网络有问题");
			}
		}
		
		model.fireOnUpdated();
		return false;
	}
}
